class ForwardList:
    '''no sentry link,FIFO.
    position list of sigel link,
    add() method to push a item to the first,
    after() method to get the next item,
    add_after() add a item to the next item,
    pop() method to pop a item from the first,
    pop_after() pop a item from the next item.
    '''

    class Position:
        """positon list seal"""
        __slots__ = '_node', '_cantainer'

        def __init__(self, cantainer, node):
            self._node = node
            self._cantainer = cantainer

        def __eq__(self, other):
            """Return True if other is a Position representing the same location."""
            return type(other) is type(self) and other._node is self._node

        def __ne__(self, other):
            """Return True if other does not represent the same location."""
            return not (self == other)

        def element(self):
            '''return the value of the position calss'''
            return self._node._value

    class _Node:
        '''node list seal'''
        __slots__ = '_value', '_next'

        def __init__(self, value):
            self._value = value
            self._next = None

    def __init__(self):
        '''creat a link'''
        self._head = None
        self.tail = None

    def _validate(self, p):
        """Return position's node, or raise appropriate error if invalid."""
        if not isinstance(p, self.Position):
            raise TypeError('p must be proper Position type')
        if p._cantainer is not self:
            raise ValueError('p does not belong to this container')
        return p._node

    def _make_position(self, node):
        '''seal the node'''
        return self.Position(self, node)

    def add(self, e):
        '''add e to the first of link'''
        if self._head == None:  # exception case
            self._head = self._Node(e)
            self._tail = self._head
            # 7.32 将首节点的下一个指针指向本身，即可构成一个循环链表
            # self._tail._next = self._head

            return self._make_position(self._tail)
        else:
            temp = self._Node(e)
            # 7.32 将新的尾节点的下一个元素指向首节点
            # temp._next = self._head

            self._tail._next = temp  # exchang next node for e
            self._tail = temp
            return self._make_position(temp)

    def after(self, p):
        p = self._validate(p)
        return self._make_position(p._next)

    def first(self):
        '''return the head item '''
        return self._make_position(self._head)

    def add_after(self, p, e):
        p = self._validate(p)  # apart the position lisk
        e = self._Node(e)  # seal the node
        e._next = p._next
        p._next = e
        return self._make_position(e)

    def pop(self):
        '''pop the fitst item of the link'''
        if self._head == None:
            raise Exception('empty')
        temp = self._head
        self._head = self._head._next
        temp._next = None
        # 7.32 删除头节点后更新尾节点的指针
        # self._tail._next = self._head
        return self._make_position(temp)

    def pop_after(self, p):
        '''pop the item after p'''
        p = self._validate(p)
        temp = p._next
        p._next = temp._next  # hop temp item
        temp._next = None
        return self._make_position(temp)

a = ForwardList()
for i in range(10):
    a.add(i)
#7.31 测试
print(a.pop_after(a.first()).element())
a.add_after(a.first(), 999)
for i in range(10):
    print(a.pop().element())
